package com.sk.bilibili.model.activity;

import android.text.SpannableStringBuilder;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.bean.Danmu;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.DanmuMapper;
import com.sk.bilibili.mapper.HistoryMapper;
import com.sk.bilibili.mapper.MemberMapper;
import com.sk.bilibili.mapper.VideoSetMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.DanmuUtils;
import com.sk.bilibili.utils.DataUtils;
import com.sk.bilibili.utils.PreferenceUtils;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.utils.StringUtils;
import com.sk.bilibili.view.activity.LoginActivity;
import com.sk.bilibili.view.activity.VideoSetActivity;

import java.util.List;

public class VideoSetModel extends BaseModel<VideoSetActivity> {

    @Autowired
    VideoSetMapper videoSetMapper;
    @Autowired
    DanmuMapper danmuMapper;
    @Autowired
    HistoryMapper historyMapper;

    public void loadVideoSet(Integer videoSetId) {
        RequestUtils.process(videoSetMapper.getVideoSet(videoSetId), videoSet ->
                view.loadVideoSetSuccess(videoSet));
    }

    public void loadDanmu(Integer videoSetId) {
        RequestUtils.process(danmuMapper.getAll(videoSetId), danmus ->
                view.loadDanmuSuccess(danmus));
    }

    public Danmu sendDanmu(String content, Integer videoSetId, Float time) {
        Danmu danmu = new Danmu();
        danmu.setColor(DanmuUtils.getRandomColor());
        danmu.setVideoId(videoSetId);
        danmu.setContent(content);
        danmu.setTime(time);
        danmu.setType(DanmuUtils.getRandomType());
        danmu.setMemberId(DataUtils.getMemberId());
        RequestUtils.process(danmuMapper.add(danmu), unused -> view.sendDanmuSuccess());
        return danmu;
    }

    public void addHistory(VideoSet videoSet) {
        JsonMap map = new JsonMap();
        map.put("type",0);
        map.put("targetId",videoSet.getId());
        map.put("title",videoSet.getTitle());
        RequestUtils.process(historyMapper.add(map));
    }

}